//
// Created by ChenJi on 2020/12/2.
//

#include "sound.h"
#include "usart_extern.h"

uint8_t isWaitingForPlay = 0;
uint8_t waitingForPlayID = 1;

uint8_t sound1[6] = {0xAA, 0x07, 0x02, 0x00, 0x01, 0xB4};
uint8_t sound2[6] = {0xAA, 0x07, 0x02, 0x00, 0x02, 0xB5};
uint8_t sound3[6] = {0xAA, 0x07, 0x02, 0x00, 0x03, 0xB6};
uint8_t sound4[6] = {0xAA, 0x07, 0x02, 0x00, 0x04, 0xB7};
uint8_t sound5[6] = {0xAA, 0x07, 0x02, 0x00, 0x05, 0xB8};
uint8_t sound6[6] = {0xAA, 0x07, 0x02, 0x00, 0x06, 0xB9};
uint8_t sound7[6] = {0xAA, 0x07, 0x02, 0x00, 0x07, 0xBA};
uint8_t sound8[6] = {0xAA, 0x07, 0x02, 0x00, 0x08, 0xBB};
uint8_t sound9[6] = {0xAA, 0x07, 0x02, 0x00, 0x09, 0xBC};
uint8_t sound10[6] = {0xAA, 0x07, 0x02, 0x00, 0x0A, 0xBD};
extern UART_HandleTypeDef huart5;

void onSoundMainThreadExecute() {
    if (isWaitingForPlay) {
        isWaitingForPlay = 0;
        playSound(waitingForPlayID);
    }
}

void playSoundAsync(uint8_t sound_id) {
    isWaitingForPlay = 1;
    waitingForPlayID = sound_id;

}

void playSound(uint8_t sound_id) {

    switch (sound_id) {
        case 1:
            sendData(sound1, &huart5, 6);
            break;
        case 2:
            sendData(sound2, &huart5, 6);
            break;
        case 3:
            sendData(sound3, &huart5, 6);
            break;
        case 4:
            sendData(sound4, &huart5, 6);
            break;
        case 5:
            sendData(sound5, &huart5, 6);
            break;
        case 6:
            sendData(sound6, &huart5, 6);
            break;
        case 7:
            sendData(sound7, &huart5, 6);
            break;
        case 8:
            sendData(sound8, &huart5, 6);
        case 9:
            sendData(sound9,&huart5,6);
        case 10:
            sendData(sound10,&huart5,6);
            break;
    }
}

void setVOL(uint8_t vol) {
    uint8_t command[5] = {0xAA, 0x13, 0x01, vol, 0xAA + 0x13 + 0x01 + vol};
    sendData(command, &huart5, sizeof(command));
}